package com.jiangjiang.eshop.inventory.async;

import com.alibaba.fastjson.JSON;
import com.jiangjiang.eshop.common.help.DateHelper;
import com.jiangjiang.eshop.inventory.domain.StockUpdateMessageEntity;
import com.jiangjiang.eshop.inventory.service.StockUpdateMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName OfflineStorageManagerImpl
 * @Description TODO
 * @Author jhw
 * @Date 2021/6/14 17:26
 * @Version 1.0
 **/
@Service
public class OfflineStorageManagerImpl implements OfflineStorageManager {

    @Autowired
    private StockUpdateMessageService stockUpdateMessageService;
    @Autowired
    private DateHelper dateHelper;

    /**
     * 是否触发离线存储的标识
     * TODO 此处非线程安全 需要后续处理
     */
    private Boolean offline = false;

    /**
     * @param message 库存更新消息
     * @throws Exception
     */
    @Override
    public void store(StockUpdateMessage message) throws Exception {
        StockUpdateMessageEntity entity = convertMessageEntity(message);
        stockUpdateMessageService.save(entity);

    }

    /**
     * 创建库存更新消息的entity对象
     *
     * @param message
     * @return
     * @throws ParseException
     */
    private StockUpdateMessageEntity convertMessageEntity(StockUpdateMessage message) throws ParseException {
        return StockUpdateMessageEntity.builder()
                .messageId(message.getId())
                .operation(message.getOperation())
                .parameter(JSON.toJSONString(message.getParameter()))
                .parameterClazz(message.getParameter().getClass().getName())
                .gmtCreate(dateHelper.getCurrentDate())
                .gmtModified(dateHelper.getCurrentDate())
                .build();

    }


    /**
     * 获取离线存储标识
     *
     * @return
     */
    @Override
    public Boolean getOffline() {
        return offline;
    }

    /**
     * 设置离线存储标识
     *
     * @param offline
     */
    @Override
    public void setOffline(Boolean offline) {
        this.offline = offline;
    }

    @Override
    public OfflineStorageIterator iterator() throws Exception {
        return new OfflineStorageIteratorImpl();
    }

    /**
     * 离线数据迭代器实现
     */
    public class OfflineStorageIteratorImpl implements OfflineStorageIterator {

        @Override
        public Boolean hasNext() {
            return stockUpdateMessageService.count() != 0;
        }

        @Override
        public List<StockUpdateMessage> next() throws Exception {
            List<StockUpdateMessageEntity> entities = stockUpdateMessageService.listTop50();
            List<StockUpdateMessage> stockUpdateMessages = new ArrayList<>();
            for (StockUpdateMessageEntity entity : entities) {
                StockUpdateMessage message = StockUpdateMessage.builder()
                        .id(entity.getMessageId())
                        .operation(entity.getOperation())
                        .parameter(JSON.parseObject(entity.getParameter(), Class.forName(entity.getParameterClazz())))
                        .build();
                stockUpdateMessages.add(message);
            }
            return stockUpdateMessages;
        }
    }

    @Override
    public void removeByBatch(List<StockUpdateMessage> messages) {
        List<String> messageIds = messages.stream().map(StockUpdateMessage::getId).collect(Collectors.toList());
        stockUpdateMessageService.removeByBatch(messageIds);
    }
}
